package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.ApArticleConfigMapper;
import com.heima.article.mapper.ApArticleContentMapper;
import com.heima.article.mapper.ArticleMapper;
import com.heima.article.service.ApArticleFreeMarkerService;
import com.heima.article.service.ApArticleService;
import com.heima.model.article.dtos.ArticleDto;
import com.heima.model.article.dtos.ArticleHomeDto;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.pojos.ApArticleConfig;
import com.heima.model.article.pojos.ApArticleContent;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import freemarker.template.Configuration;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
@Slf4j
public class ApArticleServiceImpl extends ServiceImpl<ArticleMapper, ApArticle> implements ApArticleService {

    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private ApArticleConfigMapper apArticleConfigMapper;

    @Autowired
    private ApArticleContentMapper apArticleContentMapper;

    @Autowired
    private Configuration configuration;

    @Autowired
    private ApArticleFreeMarkerService apArticleFreeMarkerService;



    @Override
    public ResponseResult load(ArticleHomeDto dto, Integer type) {

        Integer size = dto.getSize();
        if (size==null ||size==0){
            dto.setSize(10);
        }
        if (type!=1&&type!=2 ){
            type=1;
        }
        if (StringUtils.isBlank(dto.getTag())){
            dto.setTag("__all__");
        }
        if (dto.getMinBehotTime()==null) dto.setMinBehotTime(new Date());
        if (dto.getMaxBehotTime()==null) dto.setMaxBehotTime(new Date());

        List<ApArticle> articleList = articleMapper.getArticleList(dto, type);
        return ResponseResult.okResult(articleList);
    }

    @Override
    @Transactional
    public ResponseResult saveArticle(ArticleDto articleDto) {
        if (articleDto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        ApArticle apArticle = new ApArticle();
        BeanUtils.copyProperties(articleDto,apArticle);
        if (articleDto.getId()==null){

            //插入文章信息
            articleMapper.insert(apArticle);

            ApArticleConfig apArticleConfig = new ApArticleConfig();
            apArticleConfig.setArticleId(apArticle.getId());
            apArticleConfig.setIsComment(true);
            apArticleConfig.setIsDown(false);
            apArticleConfig.setIsForward(true);
            apArticleConfig.setIsDelete(false);
            //插入文章配置信息
            apArticleConfigMapper.insert(apArticleConfig);

            ApArticleContent apArticleContent = new ApArticleContent();
            String content = articleDto.getContent();
            apArticleContent.setContent(content);
            apArticleContent.setArticleId(apArticle.getId());
            //插入内容信息与关联文章信息
            apArticleContentMapper.insert(apArticleContent);

        }else {
//            ApArticle apArticleItem = articleMapper.selectOne(new LambdaQueryWrapper<ApArticle>()
//                    .eq(ApArticle::getId, articleDto.getId()));
//            BeanUtils.copyProperties(articleDto,apArticleItem);
            articleMapper.updateById(apArticle);

            ApArticleContent apArticleContent = apArticleContentMapper.selectOne(new LambdaQueryWrapper<ApArticleContent>()
                    .eq(ApArticleContent::getArticleId, articleDto.getId()));
            apArticleContent.setContent(articleDto.getContent());
            apArticleContentMapper.updateById(apArticleContent);
        }

        //异步调用，生成静态文件,并上传到minio中，
        apArticleFreeMarkerService.buildArticleToMinio(apArticle,articleDto.getContent());

        return ResponseResult.okResult(apArticle.getId());
    }
}
